/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.utfpr.backuptools.BackupTools.view.telas.cadastro.backup;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import br.edu.utfpr.backuptools.BackupTools.controller.validator.RetornoValidacao;
import br.edu.utfpr.backuptools.BackupTools.model.pojo.Backup;
import br.edu.utfpr.backuptools.BackupTools.util.enums.TipoConexao;
import br.edu.utfpr.backuptools.BackupTools.view.action.AbstractAction;
import br.edu.utfpr.backuptools.BackupTools.view.action.backup.SalvarBackupAction;
import br.edu.utfpr.backuptools.BackupTools.view.telas.principal.JanelaPrincipal;

public class CadastroBackup extends javax.swing.JFrame {

    AbstractAction<Backup> action = new SalvarBackupAction();
	Backup backup = new Backup();
	JanelaPrincipal pai;

	public CadastroBackup(JanelaPrincipal pai) {
		initComponents();
		this.pai = pai;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        radioGrupo = new javax.swing.ButtonGroup();
        jpCadastroBackup = new javax.swing.JPanel();
        lbDescricao = new javax.swing.JLabel();
        lbOrigem = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaDescricao = new javax.swing.JTextArea();
        edCaminhoOrigem = new javax.swing.JTextField();
        btSelecionarOrigem = new javax.swing.JButton();
        btSelecionarLocalBackup = new javax.swing.JButton();
        edCaminhoBackup = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jrConexaoLocal = new javax.swing.JRadioButton();
        jrConexaoRemota = new javax.swing.JRadioButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
      
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de backup");

        jpCadastroBackup.setBackground(new java.awt.Color(255, 255, 255));
        jpCadastroBackup.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbDescricao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbDescricao.setText("Descrição:");

        lbOrigem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbOrigem.setText("Caminho da pasta:");

        jtaDescricao.setColumns(20);
        jtaDescricao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jtaDescricao.setRows(5);
        jScrollPane1.setViewportView(jtaDescricao);

        edCaminhoOrigem.setEditable(false);
        edCaminhoOrigem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        btSelecionarOrigem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btSelecionarOrigem.setText("Selecionar");

        btSelecionarLocalBackup.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btSelecionarLocalBackup.setText("Selecionar");

        edCaminhoBackup.setEditable(false);
        edCaminhoBackup.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Local do backup:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Conexão:");

        jrConexaoLocal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrConexaoLocal.setText("Local");
        jrConexaoLocal.setSelected(true);
        radioGrupo.add(jrConexaoLocal);
        
        jrConexaoRemota.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrConexaoRemota.setText("Remota");
        radioGrupo.add(jrConexaoRemota);

        javax.swing.GroupLayout jpCadastroBackupLayout = new javax.swing.GroupLayout(jpCadastroBackup);
        jpCadastroBackup.setLayout(jpCadastroBackupLayout);
        jpCadastroBackupLayout.setHorizontalGroup(
            jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpCadastroBackupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lbOrigem)
                        .addComponent(lbDescricao, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jrConexaoRemota)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jrConexaoLocal)
                    .addGroup(jpCadastroBackupLayout.createSequentialGroup()
                        .addComponent(edCaminhoBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btSelecionarLocalBackup))
                    .addGroup(jpCadastroBackupLayout.createSequentialGroup()
                        .addComponent(edCaminhoOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btSelecionarOrigem)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jpCadastroBackupLayout.setVerticalGroup(
            jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpCadastroBackupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbDescricao)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpCadastroBackupLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jrConexaoLocal)
                        .addGap(8, 8, 8)
                        .addComponent(jrConexaoRemota)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpCadastroBackupLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addGap(27, 27, 27)))
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbOrigem)
                    .addComponent(btSelecionarOrigem)
                    .addComponent(edCaminhoOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpCadastroBackupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(btSelecionarLocalBackup)
                    .addComponent(edCaminhoBackup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        
        btSelecionarOrigem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	selecionarArquivosActionPerformed(evt, edCaminhoOrigem);
            }
        });
        
        btSelecionarLocalBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	selecionarArquivosActionPerformed(evt, edCaminhoBackup);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/Alarm-Error-icon.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jButton1ActionPerformed(evt);
            }
        });

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/Floppy-Small-icon.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.setPreferredSize(new java.awt.Dimension(95, 25));
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	btSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpCadastroBackup, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jpCadastroBackup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(btSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
    
    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                           
    	 backup.setDescricao(jtaDescricao.getText());
     	 backup.setOrigem(edCaminhoOrigem.getText());
         backup.setDestino(edCaminhoBackup.getText());
         
         if(jrConexaoLocal.isSelected())
        	 backup.setTipoConexao(TipoConexao.LOCAL);
         else if(jrConexaoRemota.isSelected())
        	 backup.setTipoConexao(TipoConexao.REMOTA);
         
         RetornoValidacao rv = action.execute(backup);
         
         JanelaPrincipal.addLog(rv.isOk() ? "Backup "+backup.getDescricao()+" cadastrado com sucesso" : rv.getErro());	
         JOptionPane.showMessageDialog(this, rv.isOk() ? "Backup cadastrado com sucesso" : rv.getErro());
         
         if(rv.isOk()){
        	 pai.populaTabela();
        	 dispose();
         }
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    	this.dispose();
    }
    
    /**
     * Metodo que invoca o FileChooser e captura o caminho de uma pasta selecionada
     * 
     * @param evt ActionEvent do botao que chamou
     * @param campo TextField para qual o caminho deve ir
     */
    private void selecionarArquivosActionPerformed(java.awt.event.ActionEvent evt, JTextField campo) {                                           
    	JSelecionador = new JFileChooser(System.getProperty("user.dir"));  
    	JSelecionador.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    	JSelecionador.setMultiSelectionEnabled(false);  

        int returnVal = JSelecionador.showDialog(null,"Selecionar");  
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {  
        	campo.setText(JSelecionador.getSelectedFile().getAbsolutePath());  	
        } else if (returnVal == JFileChooser.CANCEL_OPTION) {  
        	return;  
        } else if (returnVal == JFileChooser.UNDEFINED_CONDITION) {  
        	return;  
        }  
    }

  
    // Variables declaration - do not modify
	private javax.swing.JFileChooser JSelecionador;
	private javax.swing.JButton btSelecionarLocalBackup;
    private javax.swing.JButton btSelecionarOrigem;
    private javax.swing.ButtonGroup radioGrupo;
    private javax.swing.JTextField edCaminhoBackup;
    private javax.swing.JTextField edCaminhoOrigem;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jtaDescricao;
    private javax.swing.JPanel jpCadastroBackup;
    private javax.swing.JRadioButton jrConexaoLocal;
    private javax.swing.JRadioButton jrConexaoRemota;
    private javax.swing.JLabel lbDescricao;
    private javax.swing.JLabel lbOrigem;
    // End of variables declaration
}
